package com.example.icedcap.topeka_fake.widget.quiz;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.icedcap.topeka_fake.model.Category;
import com.example.icedcap.topeka_fake.model.quiz.FillTwoBlanksQuiz;

/**
 * Author: doushuqi
 * Date: 15/7/18
 * Time: 下午4:00
 * Email: shuqi.dou@singuloid.com
 */
public class FillTwoBlanksQuizView extends TextInputQuizView<FillTwoBlanksQuiz> {
    private static final String KEY_ANSWER_ONE = "ANSWER_ONE";
    private static final String KEY_ANSWER_TWO = "ANSWER_TWO";
    private static final LinearLayout.LayoutParams sLayoutParams
            = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
    private EditText mAnswerOne;
    private EditText mAnswerTwo;

    public FillTwoBlanksQuizView(Context context, Category category, FillTwoBlanksQuiz quiz) {
        super(context, category, quiz);
    }

    @Override
    protected View createQuizContentView() {
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        mAnswerOne = createEditText();
        mAnswerOne.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        mAnswerTwo = createEditText();
        addEditText(layout, mAnswerOne);
        addEditText(layout, mAnswerTwo);
        return layout;
    }

    private void addEditText(LinearLayout layout, EditText editText) {
        layout.addView(editText, sLayoutParams);
    }

    @Override
    protected boolean isAnswerCorrect() {
        return getQuiz().isAnswerCorrect(new String[]
                {mAnswerOne.getText().toString(), mAnswerTwo.getText().toString()});
    }

    @Override
    public Bundle getUserInput() {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_ANSWER_ONE, mAnswerOne.getText().toString());
        bundle.putString(KEY_ANSWER_TWO, mAnswerTwo.getText().toString());
        return bundle;
    }

    @Override
    public void setUserInput(Bundle savedInput) {
        if (null == savedInput) {
            return;
        }
        mAnswerOne.setText(savedInput.getString(KEY_ANSWER_ONE));
        mAnswerTwo.setText(savedInput.getString(KEY_ANSWER_TWO));
    }
}
